class SimulationBody < ActiveRecord::Base
	belongs_to :simulation
	has_many :simulation_data
	validates_uniqueness_of :name, :scope => [:simulation_id, :body_type_id]
	
	def self.add_planets(simulation)
		system = System.find(simulation.system_id)
    body_type_id = BodyType.find(:first, :conditions => { :name => "planet" }).id
		system.planets.each do |planet|
			sim_planet = SimulationBody.new
			sim_planet.simulation_id = simulation.id
			sim_planet.body_type_id = body_type_id
			sim_planet.name = planet.name
			sim_planet.mass = planet.mass
			sim_planet.eccentricity = planet.eccentricity
			sim_planet.semi_major_axis = planet.semi_major_axis
			sim_planet.save
		end
	end
	
	def self.create_test_particles(simulation)
    small_body_parameter = simulation.small_body_parameter
    body_type_id = BodyType.find(:first, :conditions => { :name => "test_particle" }).id
  	
		sim_bodies = SimulationBody.find(:all, :conditions => { :simulation_id => simulation.id, :body_type_id => body_type_id })
		sim_bodies.each { |sim_body| sim_body.destroy }

	  hab_step = (small_body_parameter.habitable_zone_end - small_body_parameter.habitable_zone_start) / small_body_parameter.habitable_zone_density.to_f
	  ecc_step = (small_body_parameter.eccentricity_end - small_body_parameter.eccentricity_start) / small_body_parameter.eccentricity_density.to_f
	  
	  i=0
	  small_body_parameter.habitable_zone_start.step(small_body_parameter.habitable_zone_end, hab_step) do |h| 
	  	small_body_parameter.eccentricity_start.step(small_body_parameter.eccentricity_end, ecc_step) do |e|
				SimulationBody.create( :name => "TP#{i}", :simulation_id => small_body_parameter.simulation_id, 
															 :mass => 0.0, :body_type_id => body_type_id, :eccentricity => e, 
															 :semi_major_axis => h )
				i += 1
	  	end
	  end
		return i
	end

	
end
